const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    info: null,
    logo_url: 'http://www.km567.com/hui/image/plus.png',
    pics: [],
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 'edit') {
      wx.setNavigationBarTitle({
        title: '编辑交车图片',
      })
      app.ihttpGet('https://www.58zltc.com/api.php/Car/imgItem/id/' + options.id, res => {
        this.setData({
          type: 'edit',
          info: res.data,
          pics: res.data.pics
        })
      })
    } else {
      wx.setNavigationBarTitle({
        title: '添加交车图片',
      })
      var date = new Date();
      var time = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate(); 
      this.setData({
        info: {time:time}
      })
    }

  },
  bindStartTimeChange: function(e){
    // console.log(e);
    var info = this.data.info;
    info.time = e.detail.value;
    this.setData({
      info: info
    })
  },
  bindUploadImg: function () {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + token,
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data);
            if (data.err_code > 0) {

            } else {
              var pics = that.data.pics;
              var file_name = data.data.domain;
              pics.push(file_name);
              that.setData({
                pics: pics
              })
            }
            //do something
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    // console.log(e.detail.value);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    post_data.openid = app.globalData.openid;
    post_data.pics = this.data.pics;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }
    app.ihttpPost('https://www.58zltc.com/api.php/Car/imgAdd', post_data, res => {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 500)
      }
    })
  },
})